package com.sharewithus.utlis;

import static org.apache.commons.lang.StringUtils.EMPTY;
import static org.apache.commons.lang.StringUtils.isEmpty;
import static org.apache.commons.lang.math.NumberUtils.INTEGER_ZERO;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


/**
 * Title:FormatUtil Description: 公用格式化方法
 * 
 * @Create_by: 侯铮
 * @Create_date:2011-2-9
 * @version:MMP 1.0
 * 
 */
public class FormatUtil {
	private static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

	/**
	 * 
	 * 2011-2-21, 下午02:15:25 by 侯铮 方法描述：将页面传回的日期字符串转换成java.util.date
	 * 
	 * @param str 字符串，如“1999-09-08”
	 * @return java.util.Date
	 * @throws ParseException
	 */
	public static Date StringToDate(String str) throws ParseException {
		if (isEmpty(str)) {
			return null;
		}
		try {
			return format.parse(str);
		} catch (ParseException e) {
			return null;
		}
	}

	/**
	 * 
	 * 2011-2-28, 下午12:02:07 方法描述：Date类型转String
	 * 
	 * @param date
	 * @return “yyyy-MM-dd”
	 */
	public static String DateToString(Date date) {
		return date==null?EMPTY:format.format(date);
	}
	public static String deCode(String str){
		if(isEmpty(str)){
			return EMPTY;
		}else {
			try {
				return new String(str.getBytes("GBK"),"ASCII");
			} catch (Exception e) {
				return "\u5b57\u7b26\u4e32\u8f6c\u7801\u5f02\u5e38";
			}
		}
	}
	
	public static Map<String, String> formatValueMap(Map<String, String[]> valueMap){
		Map<String, String> map = new HashMap<String, String>();
		Object[] os = valueMap.keySet().toArray();
		for (int i = 0; i < os.length; i++) {
			String key = (String)os[i];
			if(valueMap.get(key)!=null&&valueMap.get(key).length!=INTEGER_ZERO){
				map.put(key, valueMap.get(key)[INTEGER_ZERO]);
			}
		}
		return map;
	}
	
	/**
	 * 2011-7-31, 下午04:20:17
	 * 方法描述：将字符串中的特殊字符进行转义
	 * @param input 输入字符串
	 * @return  转义后的字符串
	 */
	public static String javaScriptEscape(String input) {
		  if (input == null) {
		   return input;
		  }
		  StringBuffer filtered = new StringBuffer(input.length());
		  char prevChar = '\u0000';
		  char c;
		  for (int i = 0; i < input.length(); i++) {
		   c = input.charAt(i);
		   if (c == '"') {
		    filtered.append("\\\"");
		   }
		   else if (c == '\'') {
		    filtered.append("\\'");
		   }
		   else if (c == '\\') {
		    filtered.append("\\\\");
		   }
		   else if (c == '\t') {
		    filtered.append("\\t");
		   }
		   else if (c == '\n') {
		    if (prevChar != '\r') {
		     filtered.append("\\n");
		    }
		   }
		   else if (c == '\r') {
		    filtered.append("\\n");
		   } else if (c == '\f') {
		                filtered.append("\\f");
		  } else if (c == '/') {
		                filtered.append("\\/");
		            }
		   else {
		    filtered.append(c);
		   }
		   prevChar = c;
		  }
		  return filtered.toString();
		 }


}
